package com.kcwl.carrier.domain.service;

import com.kcwl.carrier.domain.condition.CarrierMapCondition;
import com.kcwl.carrier.domain.entity.CarrierMappingInfo;
import com.kcwl.carrier.domain.repository.ICarrierMappingRepository;
import com.kcwl.carrier.infrastructure.repository.po.CarrierMappingPO;
import com.kcwl.carrier.interfaces.query.CarrierListQuery;
import com.kcwl.carrier.interfaces.query.CarrierMappingQuery;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.infrastructure.api.CommonCode;
import com.kcwl.framework.utils.KcBeanConverter;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class CarrierMappingDomainService {

    @Resource
    private ICarrierMappingRepository carrierMappingRepository;

    public void addCarrierMapping(CarrierMappingInfo carrierMapping) {
        carrierMappingRepository.save(KcBeanConverter.toBean(carrierMapping, CarrierMappingPO.class));
    }

    public List<CarrierMappingInfo> getMappingList(CarrierMappingInfo vehicleCon) {
        return carrierMappingRepository.getMappingList(vehicleCon);
    }

    public void updateCarrierMapping(CarrierMappingInfo carrierMapping) {
        carrierMappingRepository.updateById(KcBeanConverter.toBean(carrierMapping, CarrierMappingPO.class));
    }

    public void deleteByVehicleId(CarrierMappingInfo carrierMapping) {
        carrierMappingRepository.deleteByVehicleId(carrierMapping.getVehicleId());
    }

    public CarrierMappingInfo getCarrierMapingInfo(Long id) {
        return KcBeanConverter.toBean(carrierMappingRepository.getById(id), CarrierMappingInfo.class);
    }

    public List<CarrierMappingInfo> queryCarrierMappingInfo(CarrierMappingQuery carrierMappingQuery) {
        CarrierMappingInfo carrierMappingInfo = KcBeanConverter.toBean(carrierMappingQuery, CarrierMappingInfo.class);
        return carrierMappingRepository.queryCarrierMappingInfo(carrierMappingInfo);
    }

    public void deleteById(Long id) {
        carrierMappingRepository.removeById(id);
    }

    public CarrierMappingInfo getDriverByVehicleId(Long vehicleId, Long driverId) {
        return KcBeanConverter.toBean(carrierMappingRepository.getDriverByVehicleId(vehicleId, driverId), CarrierMappingInfo.class);
    }

    public Integer updateCarrierMappingByVehicleId(CarrierMappingInfo carrierMapping) {
        return carrierMappingRepository.updateByVehicleId(KcBeanConverter.toBean(carrierMapping, CarrierMappingPO.class));
    }

    public List<CarrierMappingInfo> getByVehicleIds(List<Long> vehicleIdList) {
        return KcBeanConverter.toList(carrierMappingRepository.queryCarrierMappingInfoList(vehicleIdList), CarrierMappingInfo.class);
    }

    public void carrierUnbind(Long id) {
        carrierMappingRepository.carrierUnbind(id);
    }


    /**
     * 通过车主id 获取绑定名下车的司机id
     *
     * @param carrierId 车主id
     * @return List<Long>
     */
    public List<Long> queryDriverIdsByCarrierId(Long carrierId) {
        Assert.notNull(carrierId, "carrierId 不能为空");
        CarrierMappingInfo carrierMappingInfo = new CarrierMappingInfo();
        carrierMappingInfo.setCarrierId(carrierId);
        carrierMappingInfo.setIsDel(YesNoEnum.NAY.getValue());
        List<CarrierMappingInfo> carrierMappingInfoList = carrierMappingRepository.queryCarrierMappingInfo(carrierMappingInfo);
        if (carrierMappingInfoList.isEmpty()) {
            return Collections.emptyList();
        }
        List<Long> result = new ArrayList<>(carrierMappingInfoList.size());
        for (CarrierMappingInfo item : carrierMappingInfoList) {
            if (Objects.nonNull(item.getDriverId())) {
                result.add(item.getDriverId());
            }
        }
        return result;
    }

    /**
     * 根据车辆相关条件和用户id查询映射信息
     */
    public List<CarrierMappingInfo> queryByVehicleParamAndUser(CarrierListQuery query) {
       return  KcBeanConverter.toList(carrierMappingRepository.queryByVehicleParamAndUser(query),CarrierMappingInfo.class);
    }
    /**
     * 根据司机相关条件和用户id查询映射信息
     */
    public List<CarrierMappingInfo> queryByCarrierParamAndUser(CarrierListQuery query) {
        return  KcBeanConverter.toList(carrierMappingRepository.queryByCarrierParamAndUser(query),CarrierMappingInfo.class);

    }

    public KcPage<CarrierMappingInfo> queryMappingPageByCondition(CarrierMapCondition condition) {
        return carrierMappingRepository.queryMappingPageByCondition(condition);
    }
}
